package com.tangcl.module_base.image

import android.app.Activity
import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions.bitmapTransform
import com.tangcl.module_base.image.glide.GlideApp
import jp.wasabeef.glide.transformations.BlurTransformation
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import java.io.File


/**
 * Created by TCL on 2021/5/8
 *
 * description:图片加载工具类
 */
class ImageLoader private constructor() {

    enum class LoadOption {
        LOAD_DEFAULT,//默认
        LOAD_AVATAR,//头像
        LOAD_BLUR,//高斯模糊
        LOAD_ROUND,//圆角图片
        LOAD_CIRCLE;//圆形图片
    }

    private object Holder {
        val holder = ImageLoader()
    }

    companion object {
        val instance = Holder.holder
    }

    /**
     * 通过url加载
     */
    fun loadImg(context: Context, url: String, imageView: ImageView, loadOption: LoadOption) {
        if (isValidContextForGlide(context)) {
            when (loadOption) {
                LoadOption.LOAD_DEFAULT -> {
                    GlideApp.with(context)
                        .load(url)
//                        .placeholder(R.mipmap.ic_launcher)
//                        .error(R.mipmap.ic_launcher)
                        .into(imageView)
                }

                LoadOption.LOAD_AVATAR -> {
                    GlideApp.with(context)
                        .load(url)
//                        .placeholder(R.mipmap.ic_launcher_round)
//                        .error(R.mipmap.ic_launcher_round)
                        .into(imageView)
                }


                LoadOption.LOAD_BLUR -> {
                    GlideApp.with(context)
                        .load(url)
                        .apply(
                            bitmapTransform(
                                BlurTransformation(
                                    24,
                                    4
                                )
                            )
                        )////模糊度(在0.0到25.0之间)，图片缩放比例(默认1)。
//                        .placeholder(R.mipmap.ic_launcher)
//                        .error(R.mipmap.ic_launcher)
                        .into(imageView)
                }
                LoadOption.LOAD_ROUND -> {
                    GlideApp.with(context)
                        .load(url)
                        .apply(bitmapTransform(RoundedCornersTransformation(12, 0)))
//                        .placeholder(R.mipmap.ic_launcher)
//                        .error(R.mipmap.ic_launcher)
                        .into(imageView)
                }
                LoadOption.LOAD_CIRCLE -> {
                    GlideApp.with(context)
                        .load(url)
                        .apply(bitmapTransform(CircleCrop()))
//                        .placeholder(R.mipmap.ic_launcher)
//                        .error(R.mipmap.ic_launcher)
                        .into(imageView)
                }
            }
        }
    }

    /**
     *动态配置占位图加载图片
     */
    fun loadImg(
        context: Context,
        url: String,
        imageView: ImageView,
        placeholderImg: Int,
        errorImg: Int
    ) {
        if (isValidContextForGlide(context)) {
            GlideApp.with(context)
                .load(url)
                .placeholder(placeholderImg)
                .error(errorImg)
                .into(imageView)
        }
    }

    /**
     * 通过文件加载图片
     */
    fun loadImgByFile(context: Context, file: File, imageView: ImageView) {
        if (isValidContextForGlide(context)) {
            GlideApp.with(context)
                .load(file)
//                .placeholder(R.mipmap.ic_launcher)
//                .error(R.mipmap.ic_launcher)
                .into(imageView)
        }
    }

    /**
     * 通过资源ID加载图片
     */
    fun loadImgByResource(context: Context, fileId: Int, imageView: ImageView) {
        if (isValidContextForGlide(context)) {
            GlideApp.with(context)
                .load(fileId)
//                .placeholder(R.mipmap.ic_launcher)
//                .error(R.mipmap.ic_launcher)
                .into(imageView)
        }
    }

    /**
     * 判断context是否为空
     */
    private fun isValidContextForGlide(context: Context?): Boolean {
        if (context == null) {
            return false
        }
        if (context is Activity) {
            if (context.isDestroyed || context.isFinishing) {
                return false
            }
        }
        return true
    }


}